package pers.cz.chat;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TrayBlinkingExample {
    public static void main(String[] args) {
        // 检查系统是否支持系统托盘
        if (!SystemTray.isSupported()) {
            System.err.println("系统托盘不被支持!");
            return;
        }

        // 创建托盘图标
        Image iconImage = Toolkit.getDefaultToolkit().getImage("D:\\Java\\JavaSource\\Jef-Chat\\chat-client\\src\\test\\resources\\email.svg"); // 替换为您的图标路径
        TrayIcon trayIcon = new TrayIcon(iconImage, "托盘闪烁示例");

        // 创建弹出菜单
        PopupMenu popupMenu = new PopupMenu();
        MenuItem exitItem = new MenuItem("退出");
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        popupMenu.add(exitItem);
        trayIcon.setPopupMenu(popupMenu);

        // 添加托盘图标到系统托盘
        try {
            SystemTray tray = SystemTray.getSystemTray();
            tray.add(trayIcon);

            // 闪烁效果
            new Thread(() -> {
                try {
                    while (true) {
                        // 改变图标（可以切换为另一个图标，或使用默认图标）
                        trayIcon.setImage(iconImage); // 亮起
                        Thread.sleep(500); // 延迟
                        trayIcon.setImage(Toolkit.getDefaultToolkit().getImage("D:\\Java\\JavaSource\\Jef-Chat\\chat-client\\src\\test\\resources\\error.svg")); // 关闭
                        Thread.sleep(500); // 延迟
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();

        } catch (AWTException e) {
            System.err.println("托盘图标无法添加.");
            e.printStackTrace();
        }
    }
}
